<template>
    <div style="margin-top: 60px;margin-left:330px;width: 300px;height: 500px;border: 0px solid red;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="学号" prop="number">
                <el-input v-model="ruleForm.number"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="gender">
                <div style="width: 170px;height: 30px;">
                    <template>
                        <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
                        <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
                    </template>
                </div>
            </el-form-item>
            <el-form-item label="宿舍" prop="dormitoryId">
                <!--提交的时候id名改为dormitoryId-->
                <el-select v-model="ruleForm.dormitoryId" placeholder="请选择宿舍">
                    <!--下面是把获取的数据遍历dormitoryList，分别获取name,与id,之后提交也是提交id的值-->
                    <el-option v-for="item in dormitoryList" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>

</template>

<script>
    export default {
        data() {
            return {
                category: null,
                ruleForm: {
                    number: '',
                    name: '',
                    gender: '男',
                    dormitoryId: ''
                },
                dormitoryList: '',
                rules: {
                    number: [
                        {required: true, message: '请输入学号', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    dormitoryId: [
                        {required: true, message: '请选择宿舍', trigger: 'change'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(_this.ruleForm)
                        axios.post('http://localhost:8181/student/save', _this.ruleForm).then(function (resp) {
                            if (resp.data.code == 0) {
                                _this.$alert(_this.ruleForm.name + '添加成功', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/studentManager')
                                    }
                                });
                            } else if (resp.data.code == -1) {
                                console.log(resp.data.code)
                                _this.$alert(_this.ruleForm.name + '添加失败宿舍人员已满请重新选择', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/studentAdd')
                                    }
                                });
                            }
                        })
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        created() {
            const _this = this
            axios.get('http://localhost:8181/dormitory/availableList').then(function (resp) {
                _this.dormitoryList = resp.data.data
            })
        }
    }
</script>
